package com.xbongbong.paas.service.dynamic.crm.strategy.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.PaasFormEntity;
import com.xbongbong.paas.log.constant.CrmLogMemoConstant;
import com.xbongbong.paas.pojo.dto.SaasFormSaveDTO;
import com.xbongbong.paas.service.dynamic.crm.strategy.AbstractDynamicStrategy;
import com.xbongbong.parent.interfaces.DynamicStrategy;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicDeleteDTO;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicRefInfoDTO;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicSaveDTO;
import com.xbongbong.saas.enums.ProjectLogEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.RefundEnum;
import com.xbongbong.saas.enums.dictionary.DictionaryEnum;
import com.xbongbong.saas.model.DataDictionaryModel;
import com.xbongbong.saas.toolbox.help.FastJsonHelper;
import com.xbongbong.sys.domain.entity.UserEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *退货退款
 *
 *@author weijianxun
 */
@Service("refundDynamicStrategyImpl")
public class RefundDynamicStrategyImpl extends AbstractDynamicStrategy implements DynamicStrategy {

    private final Logger LOG = LoggerFactory.getLogger(RefundDynamicStrategyImpl.class);

    private static final Map<String, Integer> refTextToLogTypeMap = new HashMap<>();

    static {
        refTextToLogTypeMap.put(RefundEnum.CUSTOMER_ID.getAttr(), ProjectLogEnum.CUSTOMER_REF.getSubType());
        refTextToLogTypeMap.put(RefundEnum.CONTRACT_ID.getAttr(), ProjectLogEnum.CONTRACT_REF.getSubType());
    }

    public static Map<String, Integer> getRefTextToLogTypeMap(){
        return Collections.unmodifiableMap(refTextToLogTypeMap);
    }
    @Resource
    private DataDictionaryModel dataDictionaryModel;

    @Override
    public List<Integer> type() {
        return Arrays.asList(XbbRefTypeEnum.REFUND.getCode());
    }

    @Override
    @Async("dynamicThreadPool")
    public void save(DynamicSaveDTO dynamicSaveDTO) {
        try {
            SaasFormSaveDTO saasFormSaveDTO = dynamicSaveDTO.getSaasFormSaveDTO();
            JSONObject newData = saasFormSaveDTO.getNewData();
            String corpid = saasFormSaveDTO.getCorpid();
            PaasFormDataEntity newPaasFormDataEntity = saasFormSaveDTO.getNewPaasFormDataEntity();
            UserEntity creatorUser = dynamicSaveDTO.getCreator();
            String refundReason = FastJsonHelper.getStringFromFormData(newData, RefundEnum.REFUND_REASON.getAttr());
            PaasFormEntity paasForm = getPaasForm(corpid, newPaasFormDataEntity.getFormId(), saasFormSaveDTO.getBusinessType());
            String communicateMemo = String.format(I18nMessageUtil.getMessage(CrmLogMemoConstant.DYNAMIC_USER_ADD_REFUND), creatorUser.getName(), newPaasFormDataEntity.getSerialNo());
            if (!StringUtil.isEmpty(refundReason)) {
                //获取启用的退货原因
                Map<String, String> refundReasonMap = dataDictionaryModel.getDictionaryMap4Saas(DictionaryEnum.REFUND_REASON, corpid, newPaasFormDataEntity.getFormId());
                String refundReasonStr = refundReasonMap.get(refundReason) == null ? "" : refundReasonMap.get(refundReason);
                if (!StringUtil.isEmpty(refundReasonStr)) {
                    communicateMemo += "," + RefundEnum.REFUND_REASON.getAttrName() + "\"" + refundReasonStr + "\"";
                }
            }
            saveRefUpDynamic(dynamicSaveDTO, refTextToLogTypeMap, paasForm, DynamicRefInfoDTO.initDynamicRefInfoDTO(newPaasFormDataEntity.getId(), XbbRefTypeEnum.REFUND.getCode(), newPaasFormDataEntity.getSerialNo()),
                    communicateMemo, null, ProjectLogEnum.REFUND_CREATE.getSubType());

        } catch (Exception e) {
            LOG.error("com.xbongbong.paas.service.dynamic.crm.strategy.impl.RefundDynamicStrategyImpl.save", e);
        }
    }

    @Override
    @Async("dynamicThreadPool")
    public void batchDelete(DynamicDeleteDTO dynamicDeleteDTO) {
        try {
            batchRefSaveUpDynamicByDeleteBatch(dynamicDeleteDTO, refTextToLogTypeMap, null, null, ProjectLogEnum.REFUND_DETELE.getSubType());
        } catch (Exception e) {
            LOG.error("com.xbongbong.paas.service.dynamic.crm.strategy.impl.RefundDynamicStrategyImpl.batchDelete", e);
        }
    }
}
